package com.caul.demo.hadoop.mapreduce.ext;

import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Partitioner;

/**
 * 自定义reducer分区规则
 * Created by sdliang on 2018/3/31.
 */
public class CustomPartitioner extends Partitioner<Text, CustomWritable> {

  public static final int REDUCE_NUM = 3;

  /**
   * @param key
   * @param value
   * @param numPartitions
   * @return 同一个值, 对应同一个reducer
   */
  public int getPartition(Text key, CustomWritable value, int numPartitions) {
    String keyStr = key.toString();
    if (keyStr.startsWith("131") || keyStr.startsWith("132") || keyStr.startsWith("133")) {
      return 0;//联通手机
    } else if (keyStr.startsWith("136") || keyStr.startsWith("137") || keyStr.startsWith("138")) {
      return 1;//移动手机
    }
    return 2;//未知
  }
}
